package com.xxzuibang.day11.房屋出租系统项目.view;

import com.xxzuibang.day11.房屋出租系统项目.domain.House1;
import com.xxzuibang.day11.房屋出租系统项目.service.HouseService1;
import com.xxzuibang.day11.房屋出租系统项目.utils.Utility;

public class HouseView1 {
    private boolean loop=true;
    private HouseService1 houseService1=new HouseService1(10);
    private char commond=' ';
    /**
     * 首页显示
     */
    public void mainMenu(){
        do {
            System.out.println("==========房屋出租系统==========");
            System.out.println("1.新增房源");
            System.out.println("2.查找房源");
            System.out.println("3.删除房源");
            System.out.println("4.修改房屋信息");
            System.out.println("5.房屋列表");
            System.out.println("6.退出");
            System.out.println("请选择(1-6)：");
            commond= Utility.readChar();
            switch (commond){
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("请输入正确的选择指令！");
            }
        } while (loop);
    }

    /**
     * 删除房屋功能
     */
    private void delHouse() {
        System.out.println("==========删除房屋==========");
        System.out.println("请选择待删除房屋编号(-1退出):");
        int delId=Utility.readInt();
        if(delId==-1){
            System.out.println("==========用户放弃删除房屋==========");
            return;
        }
        System.out.println("确认是否删除(Y/N),请小心选择");
        char choice=Utility.readConfirmSelection();
        if(choice=='Y'){
            if(houseService1.del(delId)){
                System.out.println("==========删除完成==========");
            }else{
                System.out.println("==========删除失败，编号不存在==========");
            }
        }else{
            System.out.println("==========用户放弃删除==========");

        }



    }


    /**
     * 修改房屋信息功能
     */
    private void updateHouse() {
        System.out.println("============修改房屋============");
        System.out.println("请选择待修改的房屋编号(-1)：");
        int updateId=Utility.readInt();
        if(updateId==-1){
            System.out.println("用户退出了修改操作");
            return;
        }
        House1 house1=houseService1.findById(updateId);
        if(house1==null){
            System.out.println("修改的房源信息不存在");
            return;
        }
        System.out.println("姓名("+house1.getName()+"):");
        String name=Utility.readString(8,"");
        if(!"".equals(name)){
            house1.setName(name);
        }
        System.out.println("电话("+house1.getPhone()+"):");
        String phone=Utility.readString(12,"");
        if(!"".equals(phone)){
            house1.setPhone(phone);
        }
        System.out.println("地址("+house1.getAddress()+"):");
        String address=Utility.readString(16,"");
        if(!"".equals(address)){
            house1.setAddress(address);
        }
        System.out.println("租金("+house1.getMonthrent()+"):");
        int monthrent=Utility.readInt(-1);
        if(monthrent!=-1){
            house1.setMonthrent(monthrent);
        }
        System.out.println("状态("+house1.getStatus()+"):");
        String status=Utility.readString(6,"");
        if(!"".equals(status)){
            house1.setStatus(status);
        }
        System.out.println("============修改完成============");
    }

    /**
     * 查找房源功能
     */
    private void findHouse() {
        System.out.println("=============查找房屋================");
        System.out.println("请输入你要查找的id：");
        int findId=Utility.readInt();
        House1 house1=houseService1.findById(findId);
        if(house1!=null){
            System.out.println(house1);
        }else {
            System.out.println("没有查找到相应的信息，id不存在");
        }
    }

    /**
     * 添加房屋功能
     */
    private void addHouse() {
        System.out.println("========添加房屋===========");
        System.out.println("姓名：");
        String name=Utility.readString(8);
        System.out.println("电话：");
        String phone=Utility.readString(12);
        System.out.println("地址：");
        String address=Utility.readString(16);
        System.out.println("月租：");
        int monthrent=Utility.readInt();
        System.out.println("状态(未出租/已出租)：");
        String status=Utility.readString(3);
        House1 newHouse=new House1(0,name,phone,address,monthrent,status);
       if(houseService1.add(newHouse)){
           System.out.println("========添加完成===========");
       }else{
           System.out.println("========添加失败===========");

       }


    }

    /**
     * 房屋列表功能
     */
    private void listHouse() {
        System.out.println("==========房屋列表===========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House1[] house1s=houseService1.list();
        for (int i = 0; i < house1s.length; i++) {
            if (house1s[i]==null){
                break;
            }
            System.out.println(house1s[i]+" ");
        }
        System.out.println("==========房屋列表完成===========");

    }

    /**
     * 退出功能
     */
    private void exit(){
        char c=Utility.readConfirmSelection();
        if(c=='Y'){
            loop=false;
        }

    }

}
